package com.nulldev.util.uri.yt.webVTT;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.nulldev.util.java.JVMUtil;
import com.nulldev.util.lang.xml.XML;
import com.nulldev.util.lang.xml.XML.Doc;
import com.nulldev.util.lang.xml.XML.Item;
import com.nulldev.util.logging.Logger;
import com.nulldev.util.logging.LoggerFactory;
import com.nulldev.util.math.TimeUtil;
import com.nulldev.util.other.ArrayUtils;
import com.nulldev.util.string.StringE;
import com.nulldev.util.uri.URIUtil;
import com.nulldev.util.uri.yt.webVTT.SubtitleParser.Subtitle;

public class WebVTTTransform {
	
	//UPDATE: Java's XML impelementation is broken, using own botched one.
	//UPDATE 2: I'm 80% tempted to mark downloadPage as deprecated due to it being broken.
	//At least it's faster than the XML implementation.
	public static String transformFromYouTube(String xmlDocument) {
		try {
			xmlDocument = xmlDocument.replaceAll(Pattern.quote("&quot;"), "\"");
			final LinkedList<Subtitle> subs = SubtitleParser.getSubtitles(xmlDocument);
			final WebVTTBuilder wvt = new WebVTTBuilder();
			for (final Subtitle node0 : subs) {
				//JVMUtil.async_println(node0.text());
				final String text = StringE.UNESCAPE_HTML4.translate(node0.text()); /*  + (requireSpaceOnEOL ? "&nbsp;" : ""))
						.replaceAll(Pattern.quote(" "), "&nbsp;" */
				wvt.add(text, node0.start(), node0.end());
				//index += 1;
			}
			return wvt.build();
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static class WebVTTBuilder {
		private static final Logger log = LoggerFactory.getLogger(true, true);
		private static final SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss.SSS");
		
		private final StringBuilder builder;
		private int index = 1;
		
		public WebVTTBuilder() {
			this.builder = new StringBuilder("WEBVTT\n\n");
		}
		
		public void add(final String text, final double from, final double to) {
			if (text == null) {
				log.error("add(): Blank entry was skipped! [text=" + text + ",from=" + from + ",to=" + to + "]");
				return;
			}
			this.builder.append(this.index + "\n");
			final String c0 = convert(from);
			final String c1 = convert(to);
			this.builder.append(c0 + " --> " + c1 + "\n");
			final String[] lines = text.split("\n");
			for (final String line : lines) {
				//this.builder.append((line.replaceAll(Pattern.quote("&quot;"), "\"")
				//		.replaceAll(Pattern.quote("&#39;"), "'")) + "\n");
				this.builder.append(line.replaceAll(Pattern.quote("&quot;"), "\"")
								.replaceAll(Pattern.quote("&#39;"), "'") + "\n"); // (StringE.UNESCAPE_HTML4.translate(line))
			}
			this.builder.append("\n");
			this.index += 1;
		}
		
		public String build() {
			return this.builder.toString();
		}
		
		private String convert(final double seconds) {
			return TimeUtil.milliToString((long) (seconds * 1000));
		}
	}
}
